package chapterSystem.client.model.entities;

import chapterSystem.client.model.entitiesInt.Brother;
import chapterSystem.client.model.entitiesInt.Unit;
import chapterSystem.client.model.enums.UnitType;

import java.util.ArrayList;
import java.util.List;

/**
 * User: Svetlana Zemlyanskaya(svetazem@yandex-team.ru)
 * Date: 4/8/12
 */
public class UnitEntity implements Unit {

    private int id;
    private String name;
    private Brother commander;
    private List<Brother> brothers;
    private String baseOutpost;
    private String location;
    private UnitType type;
    private int parentId;
    private List<Unit> subunits;

    public UnitEntity() {
    }

    public UnitEntity(final int id, final String name, final Brother commander, final String baseOutpost,
                      final String location, final UnitType type, final int parentId) {
        this.name = name;
        this.commander = commander;
        this.baseOutpost = baseOutpost;
        this.location = location;
        this.type = type;
        this.parentId = parentId;
        this.id = id;
        brothers = new ArrayList<Brother>();
        subunits = new ArrayList<Unit>();
    }

    public UnitEntity(final int id, final String name, final Brother commander, final List<Brother> brothers,
                      final String baseOutpost, final String location, final UnitType type, final List<Unit> subunits, final int parentId) {
        this.name = name;
        this.commander = commander;
        this.brothers = brothers;
        this.baseOutpost = baseOutpost;
        this.location = location;
        this.type = type;
        this.subunits = subunits;
        this.parentId = parentId;
        this.id = id;
    }

    @Override
    public int getId() {
        return id;
    }

    @Override
    public String getName() {
        return name;
    }

    @Override
    public Brother getCommander() {
        return commander;
    }

    @Override
    public List<Brother> getAllBrothers() {
        return brothers;
    }

    @Override
    public String getBaseOutpost() {
        return baseOutpost;
    }

    @Override
    public String getCurrentLocation() {
        return location;
    }

    @Override
    public UnitType getUnitType() {
        return type;
    }

    @Override
    public int getParentId() {
        return parentId;
    }

    @Override
    public List<Unit> getSubunits() {
        return subunits;
    }

    @Override
    public void addBrothers(final List<Brother> add) {
        if (brothers == null) {
            brothers = new ArrayList<Brother>();
        }
        brothers.addAll(add);

    }

    @Override
    public void addSubunits(final Unit add) {
        if (subunits == null) {
            subunits = new ArrayList<Unit>();
        }
        subunits.add(add);
    }

    @Override
    public String asHtml() {
        return " <table ALIGN=LEFT WIDTH=\"80%\" HEIGHT=\"150\" " +
                "style=\"width: 40%; height: 40%; border: 1px black;" +
                "    font-family: 'Palatino Linotype','Book Antiqua',Palatino,serif;" +
                "    font-weight: bold;font-style: italic;font-size: 2em\" >" +
               // " <colgroup> <col> </colgroup> " +
                " <tbody> <tr> <td>Name: </td> <td>" + name + "</td> </tr> " +
                " <tr> <td>Commander: </td> <td>" + commander.getName() + "</td> </tr> " +
                " <tr> <td>Type: </td> <td>" + type + "</td> </tr> " +
                " <tr> <td>Outpost: </td> <td>" + baseOutpost + "</td> </tr> " +
                " <tr> <td>Location: </td> <td>" + location + "</td> </tr> " +
                " </tbody> </table> ";
//        return "<h3>Name: " + name + "</h3>\n" +
//                        "<h3>Commander: " + commander.getName() + "</h3>\n" +
//                        "<h3>Type: " + type + "</h3>\n" +
//                        "<h3>Base Outpost: " + baseOutpost + "</h3>\n" +
//                        "<h3>Current Location: " + location + "</h3>\n";
    }

    @Override
    public boolean equals(final Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        final UnitEntity that = (UnitEntity) o;

        if (id != that.id) return false;

        return true;
    }

    @Override
    public int hashCode() {
        return id;
    }

    @Override
    public String toString() {
        return "UnitEntity{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", commander=" + commander +
                ", brothers=" + brothers +
                ", baseOutpost='" + baseOutpost + '\'' +
                ", location='" + location + '\'' +
                ", type=" + type +
                ", parentId=" + parentId +
                ", subunits=" + subunits +
                '}';
    }
}
